<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
} 

class table_thinker_wx_keyword extends discuz_table{
	public function __construct() {
        parent::__construct();
		$this->_table = 'thinker_wx_keyword';
		$this->_pk    = 'id';
	}

    public function fetch_by_id($id,$field='*') {
		return DB::fetch_first("SELECT $field FROM %t WHERE id=%d", array($this->_table, $id));
	}
    
    public function fetch_by_keyword($keyword,$field='*') {
		return DB::fetch_first("SELECT $field FROM %t WHERE keyword LIKE %s AND status=1", array($this->_table, '%'.$keyword.'%'));
	}
	
	public function fetch_all_list($field='*',$where='',$order='id') {
        $whereStr = '';
        if(!empty($where)){
            $whereStr = ' WHERE '.$where;
        }
		return DB::fetch_all("SELECT $field FROM %t $whereStr ORDER BY $order DESC", array($this->_table));
	}

	public function fetch_one($field='*',$where='',$order='id') {
		$whereStr = '';
		if(!empty($where)){
			$whereStr = ' WHERE '.$where;
		}
		return DB::fetch_first("SELECT $field FROM %t $whereStr ORDER BY $order DESC", array($this->_table));
	}

	public function fetch_by_where($where, $start = 0, $limit = 20) {
		$whereStr = '';
		if(!empty($where)){
			$whereStr = ' WHERE '.$where;
		}
		$datas = DB::fetch_all("SELECT * FROM %t $whereStr ORDER BY id DESC LIMIT %d,%d", array($this->_table, $start, $limit));
		if($datas) {
			foreach($datas as &$data) {
				$data['data'] = unserialize($data['data']);
			}
			return $datas;
		} else {
			return array();
		}
	}

	public function count_by_where($where) {
		$whereStr = '';
		if(!empty($where)){
			$whereStr = ' WHERE '.$where;
		}
		return DB::result_first("SELECT COUNT(*) FROM %t $whereStr", array($this->_table));
	}
	
	public function delete_by_id($id) {
		return DB::query("DELETE FROM %t WHERE id=%d", array($this->_table, $id));
	}

}


?>
